package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.exception.CustomException;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.comment.dtos.CommentStatusDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * ClassName: ApArticleConfigServiceImpl
 * Description:
 * date: 2023/8/16 0016 20:38
 *
 * @author zHan
 */
@Service
@Slf4j
@Transactional
public class ApArticleConfigServiceImpl extends ServiceImpl<ApArticleConfigMapper,
        ApArticleConfig> implements ApArticleConfigService {
    /**
     * 根据map修改文章配置，文章上下架
     *
     * @param map
     */
    @Override
    public void updateByMap(Map map) {
        // 0下架，1上架
        Object enable = map.get("enable");
        boolean isDown = true;
        if (enable.equals(1)) {
            isDown = false;
        }
        Long articleId = (Long) map.get("articleId");
        // 修改文章
        update(Wrappers.<ApArticleConfig>lambdaUpdate()
                .eq(ApArticleConfig::getArticleId, articleId)
                .set(ApArticleConfig::getIsDown, isDown));
    }

    /**
     * 查询文章配置
     *
     * @param articleId
     * @return
     */
    @Override
    public ResponseResult getArticleConfig(Long articleId) {
        // 参数校验
        if (articleId == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 查询
        ApArticleConfig apArticleConfig = getOne(Wrappers.<ApArticleConfig>lambdaQuery()
                .eq(ApArticleConfig::getArticleId, articleId));
        // 结果返回
        return ResponseResult.okResult(apArticleConfig);
    }

    /**
     * 修改文章配置
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateArticleConfig(CommentStatusDto dto) {
        // 默认可以评论
        boolean flag = true;
        // 转换参数
        if (dto.getOperation() == 0) {
            flag = false;
        }
        // 直接修改配置
        update(Wrappers.<ApArticleConfig>lambdaUpdate()
                .eq(ApArticleConfig::getArticleId, dto.getArticleId())
                .set(ApArticleConfig::getIsComment, flag));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
